/* 
Copyright (c) 2008 Delio Brignoli

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

#include <Debug.h>
dbg_filename_init();

#include <Assert.h>
assert_filename_init();

#include "Adac.h"
#include "AdacTesting.h"

#include "WProgram.h"

levels test_sequence[] PROGMEM = {

	IREF,IREF,IREF,IREF,
	VCC,VCC,VCC,VCC,
	VSS,VSS,VSS,VSS,

	IREF,VCC,VCC,VCC,
	IREF,VCC,VCC,IREF,
	IREF,VCC,VCC,VSS,
	IREF,VCC,IREF,VCC,
	IREF,VCC,IREF,VSS,
	IREF,VCC,VSS,IREF,
	IREF,VCC,VSS,VCC,
	IREF,VCC,VSS,VSS,

	IREF,VSS,VSS,VSS,
	IREF,VSS,VSS,VCC,
	IREF,VSS,VSS,IREF,
	IREF,VSS,VCC,IREF,
	IREF,VSS,VCC,VCC,
	IREF,VSS,VCC,VSS,
	IREF,VSS,IREF,VCC,
	IREF,VSS,IREF,VSS,

	IREF,IREF,IREF,VCC,
	IREF,IREF,IREF,VSS,
	IREF,IREF,VSS,VSS,
	IREF,IREF,VSS,VCC,
	IREF,IREF,VSS,IREF,
	IREF,IREF,VCC,IREF,
	IREF,IREF,VCC,VCC,
	IREF,IREF,VCC,VSS,

	VCC,IREF,IREF,IREF,
	VCC,VCC,VCC,IREF,
	VCC,VCC,VCC,VSS,
	VCC,VCC,IREF,VCC,
	VCC,VCC,IREF,VSS,
	VCC,VCC,VSS,IREF,
	VCC,VCC,VSS,VCC,
	VCC,VCC,VSS,VSS,

	VCC,VSS,VSS,VSS,
	VCC,VSS,VSS,VCC,
	VCC,VSS,VSS,IREF,
	VCC,VSS,VCC,IREF,
	VCC,VSS,VCC,VCC,
	VCC,VSS,VCC,VSS,
	VCC,VSS,IREF,VCC,
	VCC,VSS,IREF,VSS,

	VCC,IREF,IREF,VCC,
	VCC,IREF,IREF,VSS,
	VCC,IREF,VSS,VSS,
	VCC,IREF,VSS,VCC,
	VCC,IREF,VSS,IREF,
	VCC,IREF,VCC,IREF,
	VCC,IREF,VCC,VCC,
	VCC,IREF,VCC,VSS,

	VSS,VCC,VCC,VCC,
	VSS,VCC,VCC,IREF,
	VSS,VCC,VCC,VSS,
	VSS,VCC,IREF,VCC,
	VSS,VCC,IREF,VSS,
	VSS,VCC,VSS,IREF,
	VSS,VCC,VSS,VCC,
	VSS,VCC,VSS,VSS,

	VSS,IREF,IREF,IREF,
	VSS,VSS,VSS,VCC,
	VSS,VSS,VSS,IREF,
	VSS,VSS,VCC,IREF,
	VSS,VSS,VCC,VCC,
	VSS,VSS,VCC,VSS,
	VSS,VSS,IREF,VCC,
	VSS,VSS,IREF,VSS,

	VSS,IREF,IREF,VCC,
	VSS,IREF,IREF,VSS,
	VSS,IREF,VSS,VSS,
	VSS,IREF,VSS,VCC,
	VSS,IREF,VSS,IREF,
	VSS,IREF,VCC,IREF,
	VSS,IREF,VCC,VCC,
	VSS,IREF,VCC,VSS,

};

// #ifdef __cplusplus
//  extern "C" {
// #endif 
// 
// int log_result( int result, const char *func_name, const char *fileName, int line_no ) {
// 	if (result == ADAC_ERROR_OK) {
// 	    DEBUG_PREAMBLE(DEBUG_INFO);
// 		DEBUG_STR(DEBUG_INFO, "call to ");
// 		DEBUG_STR(DEBUG_INFO, func_name);
// 		DEBUG_STR(DEBUG_INFO, " returned ");
// 		DEBUG_INT(DEBUG_INFO, result);
// 		DEBUG_STR(DEBUG_INFO, "\n");
// 	} else {
// 	    DEBUG_PREAMBLE(DEBUG_ERROR);
// 		DEBUG_STR(DEBUG_ERROR, "call to ");
// 		DEBUG_STR(DEBUG_ERROR, func_name);
// 		DEBUG_STR(DEBUG_ERROR, " returned ");
// 		DEBUG_INT(DEBUG_ERROR, result);
// 		DEBUG_STR(DEBUG_ERROR, "\n");
// 	}
// 	return result;
// }
// 
// #ifdef __cplusplus
//  }
// #endif 

int count = 0;
int result = 0;

void conversion_complete(int conversion_count, void* priv) {
  count++;
  if (conversion_count == 0) {
    //Set the input the first time we are called (during prepare). It will be used for the next conversion
    adac_set_next_input(1);
  } else {
    //Set next input to a negative value will stop the conversion
    adac_set_next_input(-1);
  }
  result = adac_read_result();
}

int test_one_shot( void ) {
  int inputs[1] = {1};
  
  LOG_RESULT( adac_init() );
  assert(adac_get_state() == ADAC_STATE_STOPPED);
  LOG_RESULT( adac_prepare(inputs, sizeof(inputs), &conversion_complete, NULL) );
  assert(adac_get_state() == ADAC_STATE_READY);
  LOG_RESULT( adac_trigger() );
  //ASSERT(adac_get_state() == ADAC_STATE_RUNNING);
  delay(1000);
  assert(count == 2);
  count = 0;
  dbg_printf_1(info, "%d", result);
  assert(adac_get_state() == ADAC_STATE_FINISHED);
  LOG_RESULT( adac_pause() );
  assert(adac_get_state() == ADAC_STATE_FINISHED);
  LOG_RESULT( adac_stop() );
  assert(adac_get_state() == ADAC_STATE_STOPPED);
  delay(4000);

}